
//
//  DeleteDatePickerView.m
//  Daybook
//
//  Created by dllo on 16/6/5.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "DeleteDatePickerView.h"
#import "Common.h"

#define kPushTime 0.3

@interface DeleteDatePickerView ()
<
UIAlertViewDelegate
>
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong)  UIDatePicker *datePicker;
@property (nonatomic, strong) NSDateFormatter *formatter;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIAlertView *alert;
@property (nonatomic, strong) NSString *beginStr;
@property (nonatomic, strong) NSString *endStr;
@end
@implementation DeleteDatePickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        // 初始化设置
        self.isBeginClick = NO;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.35];
        [self createView];
    }
    return self;
}

- (void)setIsBeginClick:(BOOL)isBeginClick {
    _isBeginClick = isBeginClick;
    NSLog(@"isBgin: %d", isBeginClick);
    [self datePickerValueChange:_datePicker];
}

- (void)createView {
    
    // 设置时间格式转换器
    self.formatter = [[NSDateFormatter alloc] init];
    self.formatter.dateFormat = @"yyyy-MM-dd";

    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT, MAIN_WIDTH, 230)];
    [self addSubview:_bottomView];
    self.bottomView.backgroundColor = [UIColor clearColor];
    
        // 配置DatePicker
     self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, [CalculateFrameView setHeight:30], MAIN_WIDTH, [CalculateFrameView setHeight:200])];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    [self.bottomView addSubview:_datePicker];
    self.datePicker.maximumDate = [NSDate date];
//    CGRect newFrame = self.datePicker.frame;
//    newFrame.size.width = MAIN_WIDTH - 20 / 414.0 *MAIN_WIDTH;
//    self.datePicker.frame = newFrame;
    _datePicker.backgroundColor = [UIColor whiteColor];
    [self.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
    
    self.cancelButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:50], 0, [CalculateFrameView setWidth:50], [CalculateFrameView setHeight:30]);
        [self.bottomView addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"downAngle.png"] forState:UIControlStateNormal];
        tempButton.backgroundColor = [UIColor darkGrayColor];
        [tempButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        tempButton.showsTouchWhenHighlighted = NO;
        tempButton;
    });
    


}

- (void)datePickerValueChange:(UIDatePicker *)datePicker {
    NSString *tempStr = [self.formatter stringFromDate:self.datePicker.date];
    NSLog(@"teStr: %@", tempStr);
    
    if (_isBeginClick) {
        _beginStr = tempStr;
        NSLog(@"执行开始时间");
    }else {
        _endStr = tempStr;
        NSLog(@"执行结束时间");

    }
    
//    self.timeStr = [self.formatter stringFromDate:self.datePicker.date];
//    if (self.isClick) {
//        NSString *beginDateStr = [self.beginDateButton.titleLabel.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
//        NSString *endDateStr = [self.timeStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
//        
//        if (beginDateStr.integerValue > endDateStr.integerValue) {
//            self.upLabel.text = @"开始时间须小于结束时间";
//            [self.endDateButton setTitle:self.beginDateButton.titleLabel.text forState:UIControlStateNormal];
//        } else {
//            self.upLabel.text = @"请选择结束时间";
//            [self.endDateButton setTitle:self.timeStr forState:UIControlStateNormal];
//        }
//    }else {
//        self.upLabel.text = @"请选择开始时间";
//        [self.beginDateButton setTitle:self.timeStr forState:UIControlStateNormal];
//    }
}


- (void)show {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kPushTime animations:^{
        weakSelf.alpha = 1;
        weakSelf.bottomView.frame = CGRectMake(0, MAIN_HEIGHT - 230, MAIN_WIDTH, 230);
    }];
}

- (void)hide {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kPushTime animations:^{
        weakSelf.alpha = 0;
        weakSelf.bottomView.frame = CGRectMake(0, MAIN_HEIGHT, MAIN_WIDTH, 230);
    }];
}

- (void)cancelButtonAction:(UIButton *)button {
    [self hide];
    if (_isBeginClick) {
        [self.delegate datePickerBeginTime:_beginStr];
    }else {
        
        NSString *beginDateStr = [self.beginStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSString *endDateStr = [self.endStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        if (beginDateStr.integerValue > endDateStr.integerValue) {
            //    //弹出框
            self.alert = [[UIAlertView alloc] initWithTitle:@"开始时间须大于结束时间" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            _alert.alertViewStyle = UIAlertViewStyleDefault;
            [_alert show];
        }else {
           [self.delegate datePickerEndTime:_endStr];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
